import Vue from 'vue'
import Router from 'vue-router'
//获取M组件
const getMComponent = (name) => () =>
  import(`../components/Manager/${name}.vue`)
//获取U组件
const getUComponent = (name) => () =>
  import(`../components/User/${name}.vue`)
//获取公共组件
const getComponent = (name) => () =>
  import(`../components/${name}.vue`)

/*解决重复点击路由报错*/
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/Login' },//配置默认路由
    {
      path: '/M_Home', component: getMComponent("M_Home"),
      children:[
        { path: 'notice', component: getMComponent("Notice") },
        { path: 'videoCheck', component: getMComponent("videoCheck") },
        { path: 'postCheck', component: getMComponent("postCheck") },
        { path: 'feedbackManage', component: getMComponent("feedbackManage") },
        { path: 'report', component: getMComponent("Report") },
        { path: 'userList', component: getMComponent("UserList") },
        { path: 'activeUser', component: getMComponent("ActiveUser") },
        { path: 'newUser', component: getMComponent("NewUser") },

      ]
    },
    { path: '/register', component: getComponent("Register") },
    {
      path: '/bingo', component: getUComponent("U_Home"),

      children: [
        { path: 'Video', component: getUComponent("Video") },
        { path: 'Article', component: () => import('@/components/User/Article') },
        // 贴文详情页路由组件articleDeatils，通过路由传参postId（query参数）到路由详情页向后端获取贴文详情展示
        { path: 'articleDetails', component: () => import('@/components/User/ArticleDetails'), props: route => ({ postId: route.query.postId }) },
        { path: 'Collect', component: getUComponent("Collect") },
        { path: 'Crac', component: getUComponent("Crac") },
        { path: 'Msg', component: getUComponent("Msg") },
        { path: 'Hst', component: getUComponent("Hst") },
        {
          path: 'User_home', component: getUComponent("User_home"),
          children: [
            { path: 'upload', component: getUComponent('Upload'),
            children:[
                { path: 'my-video', component: getUComponent('MyVideo') },
                { path: 'up-video', component: getUComponent('UpVideo') },
                { path: 'my-post', component: getUComponent('MyPost') },
                { path: 'up-post', component: getUComponent('UpPost'),props: route => ({ postId: route.query.postId }) },//postId用于修改个人贴文时向后端查询回显个人贴文
              ]},
            { path: 'collect', component: getUComponent('Collect') },
            { path: 'dongtai', component: getUComponent('Dongtai') },
            { path: 'subscribe', component: getUComponent('Subscribe') },
            {
              path: 'setting', component: getUComponent('Setting'),
              children: [
                { path: 'person-info', component: getUComponent('PersonInfo') },
                { path: 'account-security', component: getUComponent('AccountSecurity') },
                { path: 'my-post', component: getUComponent('MyPost') },
                { path: 'my-video', component: getUComponent('MyVideo') },
                { path: 'my-history', component: getUComponent('MyHistory') },
              ]
            },
            // { path: 'setting', component: getUComponent('Setting') },
            { path: 'praise', component: getUComponent('Praise') },
          ]
        },

      ]
    },
    { path: '/Login', component: getComponent("Login") },
    { path: '/error', component: getComponent("error") },
    { path: '/play', component: getUComponent("play") }//注册播放（视频详情）路由



  ],
  mode: "history"
})

/**
 * 配置路由前置守卫
 */
// router.beforeEach((to,from,next)=>{
//   console.log("前置守卫启动")
//   if(to.path ==='/' || to.path === '/Login'){
//     //免检
//     return next('Login')
//   }
//   const token = window.sessionStorage.getItem('token')
//   console.log(token)
//   if(!token){
//     //令牌失效
//     console.log('token失效')
//     next('/Login')
//   }
//   next()              //调用业务路由
// })

export default router
